<template>
  <div class="category">
    <!-- 按钮开始 -->
        <el-button @click="showHandler" size='small' style="background-color: #2cb5ac;" type="primary">添加</el-button>
    <!-- 按钮结束 -->
    <!-- 表格开始 -->
        <el-table
        :data="categorys"
        style="width: 100%">
        <el-table-column
            prop="id"
            label="编号"
            width="90"
            align='center'>
        </el-table-column>
        <el-table-column
            prop="name"
            label="名称" align='center'>
        </el-table-column>
        <el-table-column
        label="图标"
        width="180">
        <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
                <img width="100%" :src="scope.row.icon">
                <div slot="reference" class="name-wrapper">
                    <img width="30px" :src="scope.row.icon">
                </div>
            </el-popover>
        </template>
        </el-table-column>
        <el-table-column prop="num" label="序号" align='center'>
        </el-table-column>
        <el-table-column width='160px' label="操作" align='center'>
            <template slot-scope="scope">
                <el-button @click='editHandler(scope.row)' style="background-color: #fff;color:#49b99d;border:none" size="mini" type="primary">编辑</el-button>
                <el-button @click="deleteHandler(scope.row.id)" style="background-color: #fff;color:red;border:none" size="mini" type="danger">删除</el-button>
            </template>
        </el-table-column>
        </el-table>
    <!-- 表格结束 -->
    <!-- 分页开始 -->
   <div class="block">
    <el-pagination
      @current-change="changeHandler"
      :page-sizes="[2, 5, 10, 15,20]"
      :page-size="3"
      layout="total, sizes, prev, pager, next, jumper"
      :total="6">
    </el-pagination>
  </div>
    <!-- 分页结束 -->
    <!-- 模态框开始 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <!-- 表单开始 -->
        <!-- {{ruleForm}} -->
        <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="栏目名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="序号" prop="name">
            <el-input v-model="ruleForm.num"></el-input>
          </el-form-item>
          <el-form-item label="图标" prop="name">
            <el-upload
                class="avatar-uploader"
                action="http://81.69.24.232/pet/jz_upload.php"
                :show-file-list="false"
                :on-success="successHandler">
                <img v-if="icon" :src="icon" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <!-- 表单结束 -->
        <div slot="footer" class="dialog-footer">
          <el-button size='mini' @click="dialogFormVisible = false">取 消</el-button>
          <el-button size='mini' type="primary" @click="submitHandler">确 定</el-button>
        </div>
      </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<!-- <script>
import {get} from '../../utils/request'
export default {
  data(){
    return {
      categorys: [],
      list:{
        page:1,
        pageSize:2
      },
      total:0
    }
  },
  methods:{
    //查询所有栏目信息
    findAllCategory(){
      //异步操作
        get('/productCategory/pageQuery',this.list).then(res=>{
          console.log(res.data,'=========');
          if(res.status === 200){
            this.categorys = res.data.list
            this.total = res.data.total
          }else {
            this.$message({
              type:'error',
              message:res.message
            })
          }
        })
    },
    //分页
    changeHandler(page){
      this.list.page = page
      this.findAllCategory()
    }
  },
  created(){
      this.findAllCategory()
  }
}
</script> -->

<script>
import {mapState,mapActions} from 'vuex'
export default {
  data(){
    return {
      list:{
        page:1,
        pageSize:3
      },
      dialogFormVisible:false,
      ruleForm:{},
      title:'添加栏目信息',
      icon:''
    }
  },
  computed:{
    ...mapState('category',['categorys','total'])
  },
  methods:{
    ...mapActions('category',['findAllCategory','saveCategory','deleteCategory']),
    //分页
    changeHandler(page){
      this.list.page = page
      this.findAllCategory(this.list)
    },
    //去添加栏目信息
    showHandler(){
      this.dialogFormVisible = true
    },
    //提交保存
    submitHandler(){
      this.saveCategory(this.ruleForm).then(res =>{
        console.log(res,'///////////////////');
        if(res.status === 200){
          this.dialogFormVisible = false
          this.findAllCategory(this.list)
          this.$message({
            type: 'success',
            message:res.message
          })
        }else {
          this.$message({
            type: 'error',
            message:res.message
          })
        }
      })
    },
    //编辑栏目信息
    editHandler(data){
      // 将当前行的内容，赋值给我们的ruleForm
        this.ruleForm = data
        this.title = '编辑栏目信息'
      //打开模态框
        this.dialogFormVisible = true
    },
    //删除栏目信息
      deleteHandler(id){
        let params = {
          id
        }
        this.deleteCategory(params).then(res =>{
          if(res.status === 200){
            this.findAllCategory(this.list)
            this.$message({
              type:'success',
              message: res.message
            })
          }else {
              this.$message({
              type:'error',
              message: res.message
            })
          }
        })
      },
      //文件上传成功时
      successHandler(file){
        //   console.log(file);
        //文件上传成功，将拿到的图片地址，保存到我们栏目信息的对象中(ruleFrom)
        this.ruleForm.icon=file
      },
},
  created(){
    this.findAllCategory(this.list)
  }
}
</script>
<style scoped>
.block{
  float:right;
}
  .avatar-uploader .el-upload {
    border: 1px dashed #7e7777;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
